<template>
  <div>
    <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible"
    >
      <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="120px"
        v-loading="loading"
      >
        <el-form-item label="场次名称" prop="name">
          <el-input v-model="dataForm.name" placeholder="场次名称"></el-input>
        </el-form-item>
        <el-form-item label="选择日期" prop="startTime">
          <div class="block">
            <el-date-picker
              v-model="dataForm.startTime"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="选择时间" prop="endTime">
          <el-select v-model="dataForm.endTime" placeholder="请选择">
            <el-option
              v-for="item in dateTime"
              :key="item.id"
              :label="getDateTime(item.startTime) + ' — ' + getDateTime(item.endTime)"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <el-switch
            v-model="dataForm.status"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import { info, add, update } from '@/api/coupon/seckill-session'
import { list } from '@/api/coupon/seckill-session-time'
export default {
  data() {
    return {
      visible: false,
      loading: false,
      dateTime: [],
      pickerOptions: {
        disabledDate(time) {
          let date = new Date()
          if(time.getYear() > date.getYear()) {
            return false;
          }
          if(time.getMonth() > date.getMonth()) {
            return false;
          }
          if(time.getYear() === date.getYear()) {
            if(time.getMonth() === date.getMonth()) {
              if(time.getDate() >= date.getDate()) {
                return false;
              }
            }
          }
          return true;
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '明天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '后天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
            picker.$emit('pick', date);
          }
        }]
      },
      dataForm: {
        id: 0,
        name: "",
        startTime: "",
        endTime: "",
        status: "",
        createTime: ""
      },
      dataRule: {
        name: [
          { required: true, message: "场次名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "每日开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "每日结束时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "启用状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.loading = true
      list({
        'page': null,
        'limit': null
      })
        .then(({data}) => {
          this.dateTime = data.list
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
            if (this.dataForm.id) {
              info(this.dataForm.id)
                .then(({ data }) => {
                  this.dataForm.name = data.name;
                  this.dataForm.startTime = data.startTime;
                  const time = new Date(data.startTime)
                  this.dateTime.forEach((item) => {

                    const startTime = new Date(item.startTime)
                    if(startTime.getHours() === time.getHours() &&
                      startTime.getMinutes() === time.getMinutes() &&
                      startTime.getSeconds() === time.getSeconds()) {
                      this.dataForm.endTime = item.id
                    }
                  })
                  this.dataForm.status = data.status;
                  this.dataForm.createTime = data.createTime;
                  this.loading = false
                });
            } else {
              this.loading = false
            }
          });
        }).catch()
    },
    // 给时间补零
    zeroFill(data) {
      return data < 10 ? "0" + data : data
    },
    getDateTime(time) {
      const startTime = new Date(time)
      const startHours = this.zeroFill(startTime.getHours())
      const startMinutes = this.zeroFill(startTime.getMinutes())
      const startSeconds = this.zeroFill(startTime.getSeconds())
      return startHours + ":" + startMinutes + ":" + startSeconds
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.dateTime.forEach((item) => {
            if(item.id === this.dataForm.endTime) {
              this.dataForm.endTime = new Date(this.dataForm.startTime.getTime())
              const startTime = new Date(item.startTime)
              this.dataForm.startTime.setHours(startTime.getHours())
              this.dataForm.startTime.setMinutes(startTime.getMinutes())
              this.dataForm.startTime.setSeconds(startTime.getSeconds())

              const endTime = new Date(item.endTime)
              this.dataForm.endTime.setHours(endTime.getHours())
              this.dataForm.endTime.setMinutes(endTime.getMinutes())
              this.dataForm.endTime.setSeconds(endTime.getSeconds())
            }
          })

          const query = {
            id: this.dataForm.id || undefined,
            name: this.dataForm.name,
            startTime: this.dataForm.startTime,
            endTime: this.dataForm.endTime,
            status: this.dataForm.status
          }
          // !this.dataForm.id ? add(query) : update(query)
          if(!this.dataForm.id) {
            add(query)
              .then(() => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.visible = false;
                this.$emit("refreshDataList");
              }).catch()
          } else {
            update(query)
              .then(() => {
                this.$message({
                  message: "操作成功",
                  type: "success"
                });
                this.visible = false;
                this.$emit("refreshDataList");
              }).catch()
          }

        }
      });
    }
  }
};
</script>
